<template>
  <div class="container">
    <!-- 卡片视图 -->
    <el-card>
      <header>
        <!--搜索区域 -->
        <!-- <search-keyword :options="searchOptions" @search="changequerInfo($event)" /> -->
        <search-keyword :options="searchOptions" :queryInfo="queryInfo" @search="loadArticleList" />
        <!-- 新增技巧 -->
        <el-button
          class="addArticleBtn"
          type="success"
          size="small"
          icon="el-icon-edit"
          @click="addArticleDialog=true"
        >新增技巧</el-button>
      </header>

      <el-alert type="info" show-icon :closable="false">
        <span slot="title">数据一共 {{ total }} 条</span>
      </el-alert>

      <!-- 文章列表 -->
      <el-table :data="articleList" stripe style="width: 100%" height:200px>
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column label="文章标题" width="390">
          <template slot-scope="scope">
            {{ scope.row.title }}
            <i
              v-if="scope.row.videoURL"
              class="el-icon-film vedioIcon"
              @click="videoShow(scope.row)"
            ></i>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column prop="createTime" label="录入时间" width="180">
          <template slot-scope="scope">{{ scope.row.createTime | formatDate }}</template>
        </el-table-column>
        <el-table-column label="状态" width="90">
          <template slot-scope="scope">{{ scope.row.state === 1 ? '以启用' : '已禁用' }}</template>
        </el-table-column>
        <el-table-column label="操作" width="190">
          <template slot-scope="scope">
            <el-button type="text" @click="previewArticle(scope.row)">预览</el-button>
            <el-button
              type="text"
              @click="changeStateHan(scope.row)"
            >{{ scope.row.state === 0 ? '启用' : '禁用' }}</el-button>
            <el-button
              type="text"
              :disabled="scope.row.state === 0"
              @click="editArticleDialogShow(scope.row)"
            >修改</el-button>
            <el-button
              type="text"
              :disabled="scope.row.state === 0"
              @click="removeArticle(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <pagination :queryInfo="queryInfo" :total="total" @pagination="loadArticleList" />
      <!-- <el-pagination
        :current-page="queryInfo.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
      ></el-pagination>-->
    </el-card>

    <!-- 添加文章对话框 -->
    <el-dialog title="新增文章" :visible.sync="addArticleDialog" width="50%">
      <articles
        v-if="addArticleDialog"
        @valid="addarticleFromRef=$event"
        @articleFrom="addarticleFrom=$event"
      />
      <span slot="footer">
        <el-button @click="clearAdd">取 消</el-button>
        <el-button type="primary" @click="addArticle">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改文章对话框 -->
    <el-dialog title="修改文章" :visible.sync="editArticleDialog" width="50%">
      <articles
        v-if="editArticleDialog"
        :articleFrom="editArticleById"
        @valid="editarticleFromRef=$event"
      />
      <span slot="footer">
        <el-button @click="clearEdit">取 消</el-button>
        <el-button type="primary" @click="editArticle">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 预览文章对话框 -->
    <el-dialog title="预览文章" :visible.sync="previewArticleDialog" width="50%">
      <h2>{{ previewContent.title }}</h2>
      <div class="articleInfo">
        <span>{{ previewContent.createTime | formatDate }}</span>
        <span>{{ previewContent.username }}</span>
        <span class="el-icon-view"></span>
        <span>{{ previewContent.visits }}</span>
      </div>
      <div class="articleBody" v-html="previewContent.articleBody"></div>
    </el-dialog>

    <!-- 视频弹框 -->
    <el-dialog
      class="vedioDialogShow"
      :visible.sync="vedioDialog"
      :close-on-click-modal="false"
      width="800px"
    >
      <video :src="videoURL" muted autoplay loop width="100%"></video>
      <div class="closeVideo" @click="vedioDialog=false">
        <i class="el-icon-close"></i>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 搜索功能
import SearchKeyword from '@/components/SearchKeyword'
import { getArticleList, addArticleList, getArticleDetail, changeState, update, remove } from '@/api/hmmm/articles'
// 添加文章
import Articles from '../components/articles-add'
import Pagination from '@/components/Pagination'
export default {
  name: 'Article',
  data() {
    return {
      queryInfo: {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        keyword: '', // 关键字
        state: null
      },
      searchOptions: {
        placeholder: '根据用文章标题搜索', // 输入框内提示信息
        sapntext1: '关键字', // label提示1
        sapntext2: '状态', // label提示2  没有状态可不写
        isStateShow: true // 是否显示状态输入框，无则不写
        // keyword: 'keyword'
      },
      total: 0, // 总数
      articleList: [], // 文章列表
      addArticleDialog: false, // 控制添加文章对话框的显示隐藏
      addarticleFromRef: '', // 添加文章表单对象
      addarticleFrom: {}, // 添加文章的值
      editArticleDialog: false, // 控制修改文章对话框的显示隐藏
      editArticleById: {}, // 根据 id 获取的文章数据
      editarticleFromRef: {}, // 修改文章表单对象
      previewArticleDialog: false, // 控制预览文章对话框显示隐藏
      previewContent: {}, // 预览文章内容
      vedioDialog: false, // 控制视频弹框的显示隐藏
      videoURL: '' // 视频地址
    }
  },
  created() {
    this.loadArticleList()
  },
  methods: {
    // 获取文章列表功能
    async loadArticleList() {
      try {
        const { data } = await getArticleList(this.queryInfo)
        this.total = data.counts
        this.articleList = data.items
        console.log(this.articleList, this.total)
      } catch (error) {
        this.$message.error('获取失败')
      }
    },

    // 确认添加文章
    addArticle() {
      console.log(this.addarticleFromRef)
      this.addarticleFromRef.validate(async valid => {
        if (!valid) return
        try {
          // console.log(this.addarticleFrom)
          await addArticleList(this.addarticleFrom)
          this.$message.success('添加成功!')
          this.loadArticleList()
        } catch (error) {
          this.$message.error('添加失败！')
        }
        this.addArticleDialog = false
      })
    },

    // 取消，清空表单，隐藏对话框
    clearAdd() {
      this.addarticleFromRef.resetFields()
      this.addArticleDialog = false
    },

    // 显示编辑对话框
    async editArticleDialogShow(row) {
      this.editArticleDialog = true
      try {
        this.editArticleById = await this.loadArticleListById(row)
      } catch (error) {
        this.$message.error('获取失败！')
      }
    },

    // 根据 id 获取文章详情
    async loadArticleListById(row) {
      const { data } = await getArticleDetail(row)
      return data
    },

    // 取消修改
    clearEdit() {
      this.editArticleDialog = false
      this.editarticleFromRef.resetFields()
    },

    // 确认修改
    editArticle() {
      this.editarticleFromRef.validate(async valid => {
        if (!valid) return
        try {
          await update(this.editArticleById)
          // console.log(this.editArticleById)
          this.editArticleDialog = false
          this.loadArticleList()
          this.$message.success('修改成功！')
        } catch (error) {
          console.log(error)

          this.$message.error('修改失败！')
        }
      })
    },

    // 修改文章状态
    async changeStateHan(row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        this.$message.success('更改成功!')
        this.loadArticleList()
      } catch (error) {
        this.$message.error('更改失败!')
      }
    },

    // 删除文章
    async removeArticle(row) {
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') return this.$message.info('您取消了删除')
      try {
        await remove(row)
        this.$message.success('删除成功！')
        this.loadArticleList()
      } catch (error) {
        this.$message.error('删除失败！')
      }
    },

    // 显示预览
    async previewArticle(row) {
      this.previewArticleDialog = true
      // this.previewContent = await this.loadArticleListById(row)
      this.previewContent = row
      console.log(this.previewContent)
    },

    // 播放视频
    async videoShow(row) {
      this.vedioDialog = true
      this.videoURL = row.videoURL
      this.previewContent = await this.loadArticleListById(row)
    }
  },
  components: {
    SearchKeyword,
    Articles,
    Pagination
  }
}
</script>

<style scoped lang='scss'>
.container {
  margin: 15px 10px;
  ::v-deep .el-table__row {
    height: 56px;
  }

  header {
    display: flex;
    justify-content: space-between;
  }
  .el-alert {
    margin: 18px 0 !important;
  }
  .el-pagination {
    float: right;
    margin: 20px 20px;
  }
  .articleInfo {
    span {
      margin-left: 10px;
    }
  }
  .articleBody {
    margin-top: 15px;
    padding: 10px;
    background-color: #f5f5f5;
    border-top: 1px dashed #ccc;
    // ::v-deep img {
    //   width: 100%;
    // }
  }
  .vedioIcon {
    color: #00f;
    font-size: 18px;
    cursor: pointer;
  }

  .vedioDialogShow {
    ::v-deep .el-dialog__header {
      display: none;
    }
    ::v-deep .el-dialog__body {
      position: relative;
      padding: 0;
      margin: 0;
      height: 450px;
    }
    .closeVideo {
      width: 50px;
      height: 50px;
      position: absolute;
      background: rgba(0, 0, 0, 0.4);
      text-align: center;
      color: #fff;
      left: 50%;
      top: 0;
      transform: translate(-50%, -100%);
      border-radius: 50%;
      line-height: 55px;
      cursor: pointer;
      i {
        font-size: 20px;
      }
    }
    ::v-deep .cell:last-child {
      min-width: 180px;
    }
  }
  ::v-deep .ql-align-center img {
    max-width: 100% !important;
  }
}
</style>
